﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System;
using System.Threading.Tasks;
using MaiPlanServer.Utility;
using MaiPlanServer.IServices.IMasterDataServices;
using MaiPlanServer.Model;
using System.Drawing;

namespace MaiPlanServer.API.Controllers.MasterData
{
    [Route("api/[controller]")]
    [ApiController]
    public class CodingRulesController : ControllerBase
    {
        public CodingRulesController(ICodingRulesServices CrDb)
        {
            this.CrDb = CrDb;
        }

        public ICodingRulesServices CrDb { get; }

        /// <summary>
        /// 获取编码规则表数据
        /// </summary>
        /// <param name="PageIndex">当前页数</param>
        /// <param name="PageSize">每页显示条数</param>
        /// <param name="ruleCode">规则编码</param>
        /// <param name="ruleName">规则名称</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetJson(int PageIndex, int PageSize, string ruleCode, string ruleName, string State)
        {
            try
            {
                var list = await CrDb.GetJsonAsync(PageIndex, PageSize, ruleCode, ruleName, State);
                var count = await CrDb.GetCountAsync(ruleName);

                return Ok(BaseState.CreateInstance(200, "请求成功", list, count));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求失败ex"));
            }
        }

        [HttpPost]
        public async Task<IActionResult> Add([FromForm] SYS_AUTO_CODE_RULE model)
        {
            try
            {
                model.RULE_ID = Guid.NewGuid();
                model.CREATE_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                var result = await CrDb.AddAsync(model);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "新增成功"));
                }
                return Ok(BaseState.CreateInstance(-1, "新增失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "新增失败，服务器异常，请联系管理员"));
            }
        }

        [HttpPut]
        public async Task<IActionResult> Put([FromForm] SYS_AUTO_CODE_RULE model)
        {
            try
            {
                var result = await CrDb.UpdateAsync(model);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "修改成功"));
                }
                return Ok(BaseState.CreateInstance(-1, "修改失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "修改失败，服务器异常，请联系管理员"));
            }
        }

        [HttpDelete]
        public async Task<IActionResult> Del(string Id)
        {
            try
            {
                var result = await CrDb.DellAsync(Id);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "删除成功"));
                }
                return Ok(BaseState.CreateInstance(-1, "删除失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "删除失败，服务器异常，请联系管理员"));
            }
        }


        [HttpDelete("DelAll")]
        public async Task<IActionResult> DelAll(string Id)
        {
            try
            {
                string[] ids = Id.Split(',');
                var result = await CrDb.DellAllAsync(ids);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "删除成功"));
                }
                return Ok(BaseState.CreateInstance(-1, "删除失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "删除失败，服务器异常，请联系管理员"));
            }
        }


    }
}
